<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$SIZE = 20;
session_name(SESSION);
session_start();
$_SESSION[TINYMCE_UPLOAD] = 'uploads/admin/';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);
$inst_id = MiscUtils::getParam('inid', 0);

$page = MiscUtils::getParam('pg', 1);
$items = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), NULL, 't.order_number', 'ASC', $page, $SIZE, NULL);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.list {
  font-size: 12px;
  width: 950px;
  margin: 5px 0;
}
table.list tr.none td {
  padding: 2px 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
}
table.form {
  background-color: #fff;
  border-top: 1px solid #222;
  border-left: 1px solid #222;
}
table.form th.header {
  text-align: left;
  color: #fff;
  background-color: #222;
  padding-left: 5px;
  font-weight: bold;
  height: 25px;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
}

table.form td {
  padding: 3px 5px; 
  vertical-align: middle;
}
table.form td.title {
  background-color: #666;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
  width: 80px;
  color: #fff;
  padding: 0 5px;
  vertical-align: top;
}
table.form td.content {
  width: 300px;
  padding: 0;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
}
table.form td.content input {
  width: 98%;
  height: 20px;
  border: 0;
  padding-left: 2px;
}
table.form4 {
  background-color: #e9e9e9;
  border: 1px solid #666;
  color: #000;
  padding: 2px;
  padding-bottom: 20px;
  cursor: pointer;
  display: inline-block;
  margin: 2px;
  font-weight: bold;
}
table.form4 td {
  padding: 0;
  spacing: 0;
}
.imgbtn {
  background-image: url(assets/imgs/add.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
}
.imgbtn:hover {
  background-color: #eee;
}
.save {
  background-image: url(assets/imgs/save.png);
  background-repeat: no-repeat;
}
.del {
  background-image: url(assets/imgs/delete.png);
  background-repeat: no-repeat;
  width: 80%;
}
object { float: left; }
</style>
</head>
<body>
<button class="imgbtn" onclick="add();">Add new</button>
<button class="imgbtn save" onclick="update();">Update</button>
<table id="items" class="list" cellPadding= 0></table>
<div id="pagination" <?php if (count($items->d) == 0) echo 'style="display: none;"' ?>></div>
<button class="imgbtn" onclick="add();">Add new</button>
<button class="imgbtn save" onclick="update();">Update</button>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../libs/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.queue.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/myupload2.js"></script>
<script type="text/javascript" src="../../../incls/js/pagination.js"></script>
<link rel="stylesheet" href="assets/css/prettyPhoto.css" type="text/css" media="screen" charset="utf-8" />
<script src="assets/js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
  var _items = JSON.parse('<?php echo addslashes(json_encode($items)); ?>');
  $(function(){
    var elm = document.getElementById('pagination');
    new ModulePagination(document, elm, null, null, null, null, {
      page: _items.p,
      total: _items.t,
      limit: _items.s,
      callbackFunc: function(page) {
        parent.location.href = 'http://<?php echo URL; ?>admin.php?i=<?php echo $page_id; ?>&pg=' + page;
      },
      contenxt: this
    });
    
    var table, tr, td;
    table = document.getElementById('items');
    MyUtils.removeTableRows(table, 0);
    for (var idx in _items.d) {
      var obj = _items.d[idx];
      tr = table.insertRow(-1);
      td = tr.insertCell(-1);
      td.className = (idx % 2 == 0) ? 'roweven' : 'rowodd';
      var itable, itr, itd, ielm;
      itable = document.createElement('table');
      td.appendChild(itable);
      itable.className = 'form';
      itable.cellPadding = 0;
      itable.cellSpacing = 0;
      itr = itable.insertRow(-1);
      itd = itr.insertCell(-1);
      itd.className = 'title';
      itd.appendChild(document.createTextNode('Link'));
      
      itd = itr.insertCell(-1);
      itd.className = 'content';
      ielm = document.createElement('input');
      ielm.type = 'text';
      ielm.value = obj.url;
      ielm._obj = obj;
      ielm.onchange = function() {
        this._obj.url = this.value;
      }
      itd.appendChild(ielm);
      itd = itr.insertCell(-1);
      itd.className = 'title';
      itd.appendChild(document.createTextNode('Rank'));
      itd = itr.insertCell(-1);
      itd.className = 'content';
      itd.style.width = '80px';
      ielm = document.createElement('input');
      ielm.type = 'text';
      ielm.value = obj.order_number;
      ielm._obj = obj;
      ielm.onchange = function() {
        this._obj.order_number = this.value;
      }
      itd.appendChild(ielm);
      itd = itr.insertCell(-1);
      itd.className = 'title';
      ielm = document.createElement('span');
      ielm.id = 'up' + obj.id;
      itd.appendChild(ielm);
      ielm = document.createElement('span');
      ielm.id = 'can' + obj.id;
      ielm.style.display = 'none';
      itd.appendChild(ielm);
      itr = itable.insertRow(-1);
      itd = itr.insertCell(-1);
      itd.rowSpan = 2;
      itd.className = 'title';
      itd.appendChild(document.createTextNode('Description'));
      ielm = document.createElement('span');
      ielm.style.color = '#aaa';
      ielm.style.fontSize = '11px';
      ielm.appendChild(document.createTextNode(' (only 1 line of text)'));
      itd.appendChild(ielm);
      itd = itr.insertCell(-1);
      itd.colSpan = 3;
      itd.rowSpan = 2;
      itd.className = 'content';
      ielm = document.createElement('textarea');
      ielm.id = 'desc' + obj.id;
      ielm.value = obj.title;
      ielm._obj = obj;
      itd.appendChild(ielm);
      itd = itr.insertCell(-1);
      itd.className = 'content';
      itd.style.borderBottom = 0;
      itd.style.height = '210px';
      itd.style.textAlign = 'center';
      itd.style.verticalAlign = 'top';
      itd.id = 'img' + obj.id;
      itd.appendChild(__get_img(obj));
      itr = itable.insertRow(-1);
      itd = itr.insertCell(-1);
      itd.className = 'content';
      itd.style.textAlign = 'center';
      ielm = document.createElement('button');
      ielm.className = 'imgbtn del';
      ielm.appendChild(document.createTextNode('Delete This Row'));
      ielm._obj = obj;
      ielm.onclick = function() {
        r = window.confirm('Are You Sure?');
        if (r) {
          remove(this._obj.id);
        }
      }
      itd.appendChild(ielm);

      new MyUpload('up' + obj.id, '<?php echo basename(__FILE__, '.php'); ?>.upload.php', function(msgs) {
        var obj, elm;
        obj = JSON.parse(msgs[0]);
        elm = document.getElementById('img' + obj.id);
        MyUtils.removeChildElements(elm);
        elm.appendChild(__get_img(obj));
        __set_pp();
      }, {
        dir: '../../../',
        button_image_url: 'images/browse.png',
        button_width: 94,
        button_height: 25,
        file_upload_limit: 1,
        file_queue_limit: 1,
        file_size_limit: '8MB',
        file_types: '*.jpg;*.jpeg;*.png;*.gif',
        file_types_description: 'Image Files',
        cancelBtnId: 'can' + obj.id,
        post_params: {
          id: obj.id
        },
        debug: 0
      });
      tinyMCE.init( {
        // General options
        mode: 'exact',
        elements: 'desc' + obj.id,
        theme: 'advanced',

        plugins: 'pdw,filebrowser,autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist',
        // Theme options
        theme_advanced_buttons1: 'pdw_toggle,|,undo,redo,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,|,forecolor,backcolor,|,image,media,|,preview',
        theme_advanced_buttons2: 'search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime',
        theme_advanced_buttons3: 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,advhr,|,print,|,ltr,rtl,|,fullscreen',
        theme_advanced_buttons4: 'insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak',
        theme_advanced_toolbar_location: 'top',
        theme_advanced_toolbar_align: 'left',
        theme_advanced_statusbar_location: 'bottom',
        theme_advanced_resizing: false,

        file_browser_callback: 'filebrowser',
        pdw_toggle_on: '1',
        pdw_toggle_toolbars: '2,3,4',
        // Example content CSS (should be your site CSS)
        theme_advanced_font_sizes: '10px,11px,12px,13px,14px,15px,16px,17px,18px,19px,20px,21px,22px,23px,24px,25px,26px,27px,28px,29px,30px',
        theme_advanced_fonts : "Andale Mono=andale mono,times;"+
                               "Arial=arial,helvetica,sans-serif;"+
                               "Arial Black=arial black,avant garde;"+
                               "Book Antiqua=book antiqua,palatino;"+
                               "Comic Sans MS=comic sans ms,sans-serif;"+
                               "Courier New=courier new,courier;"+
                               "Georgia=georgia,palatino;"+
                               "Helvetica=helvetica;"+
                               "Impact=impact,chicago;"+
                               "Symbol=symbol;"+
                               "Tahoma=tahoma,arial,helvetica,sans-serif;"+
                               "Terminal=terminal,monaco;"+
                               "Times New Roman=times new roman,times;"+
                               "Trebuchet MS=trebuchet ms,geneva;"+
                               "Verdana=verdana,geneva;"+
                               "Webdings=webdings;"+
                               "Wingdings=wingdings,zapf dingbats"
      });
    }
    if (_items.d.length == 0) {
      tr = table.insertRow(-1);
      tr.className = 'none';
      td = tr.insertCell(-1);
      td.appendChild(document.createTextNode('No Results Found'));
    }
    __set_pp();
    parent.updateHeight_<?php echo $inst_id; ?>();
  });

  function add() {
    var obj = {
      order_number: 0,
      title: '',
      url: '',
      file: ''
    };
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=add&q=' + encodeURIComponent(JSON.stringify(obj)),
      success: function(msg){
        try {
          location.reload();
        } catch (ex) {
        }
      }
    });
  }

  function update() {
    var items = [];
    for (var idx in _items.d) {
      var obj = _items.d[idx];
      items[items.length] = {
        id: obj.id,
        order_number: obj.order_number,
        url: obj.url,
        title: tinyMCE.get('desc' + obj.id).getContent()
      };
    }
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=update&q=' + encodeURIComponent(JSON.stringify(items)),
      success: function(msg){
        try {
          location.reload();
        } catch (ex) {
        }
      }
    });
  }

  function remove(id) {
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=remove&q=' + encodeURIComponent(id),
      success: function(msg){
        try {
          location.reload();
        } catch (ex) {
        }
      }
    });
  }

  function __get_img(img_obj) {
    var table, tr, td, elm, a, img;
    table = document.createElement('table');
    table.className = 'form4';
    table.cellPadding = 0;
    table.cellSpacing = 0;
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.textAlign = 'right';
    td.className = 'img';
    elm = document.createElement('img');
    elm.src = 'assets/imgs/del.png';
    elm.onclick = function() {
      var r = window.confirm('Are You Sure?');
      if (r) {
        $.ajax({
          type: "POST",
          url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
          data: "action=del_img&q=" + encodeURIComponent(JSON.stringify(img_obj)),
          success: function(msg){
            _images = JSON.parse(msg);
            var obj, elm;
            obj = JSON.parse(msg);
            elm = document.getElementById('img' + obj.id);
            MyUtils.removeChildElements(elm);
            elm.appendChild(__get_img(obj));
            __set_pp();
          }
        });
      }
    };
    td.appendChild(elm);
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    a = document.createElement('a');
    a.rel = 'prettyPhoto[gallery1]';
    if (img_obj.file != '') {
      a.href = img_obj.file;
    } else {
      a.href = 'assets/imgs/noimg.png';
    }
    td.appendChild(a);
    img = document.createElement('img');
    if (img_obj.file != '') {
      img.src = img_obj.file;
    } else {
      img.src = 'assets/imgs/noimg.png';
    }
    img.style.width = '120px';
    a.appendChild(img);
    return table;
  }

  function __set_pp() {
    $("a[rel^='prettyPhoto']").prettyPhoto({
      social_tools: ''
    });
  }
</script>
</body>
</html>
